<?php

/**
 * CCoreMimeMessageBodyStructure
 *
 * @author Usenko Timur <usenko.timur@gmail.com>
 * @package mime
 */

//CCore::Using('mime.part');

/**
 * @package mime
 */
class CCoreMimeMessageBodyStructure
{
	/**
	 * @var array
	 */
	private $_aBodyStructure;

	private $_aPlainIndexs;
	private $_aHtmlIndexs;
	private $_aAttachmentsIndexs;

	/**
	 * @param array $oMimePart
	 * @return CCoreMimeMessageBodyStructure
	 */
	public function __construct(array $aBodyStructure)
	{
		$this->_aBodyStructure = $aBodyStructure;
		
		$this->_aPlainIndexs = array();
		$this->_aHtmlIndexs = array();
		$this->_aAttachmentsIndexs = array();

		$this->_fillIndexs($this->_aBodyStructure);
	}

	/**
	 * @return array
	 */
	public function GetBodyStructure()
	{
		if (is_array($this->_aBodyStructure) && count($this->_aBodyStructure) > 1 && is_string($this->_aBodyStructure[0]) && is_string($this->_aBodyStructure[1]))
		{
			return array($this->_aBodyStructure);
		}
		
		return $this->_aBodyStructure;
	}

	/**
	 * @param array $aBodyStructure
	 * @param string $nsParentKey = null
	 * @return void
	 */
	private function _fillIndexs(array $aBodyStructure, $nsParentKey = null)
	{
		if (is_array($aBodyStructure) && count($aBodyStructure) > 1 && is_string($aBodyStructure[0]) && is_string($aBodyStructure[1]))
		{
			$aBodyStructure = array($this->_aBodyStructure);
		}
		
		foreach ($aBodyStructure as $sKey => $mValue)
		{
			if (isset($mValue[0]))
			{
				$sIdx = (null !== $nsParentKey) ? (string) ($nsParentKey.'.'.($sKey + 1)) : (string) ($sKey + 1);
				if (is_array($mValue[0]))
				{
					$this->_fillIndexs($mValue, $sIdx);
				}
				else
				{
					$type = self::_getBodyStructurePartType($mValue);
					switch ($type)
					{
						case C_MIME_BODYSTRUCTURE_TYPE_TEXT_PLAIN:
							$this->_aPlainIndexs[] = $sIdx;
							break;
						case C_MIME_BODYSTRUCTURE_TYPE_TEXT_HTML:
							$this->_aHtmlIndexs[] = $sIdx;
							break;
						case C_MIME_BODYSTRUCTURE_TYPE_ATTACHMENT:
							$this->_aAttachmentsIndexs[] = $sIdx;
							break;
					}
				}
			}
		}
	}

	/**
	 * @param array $aPart
	 * @return int
	 */
	private function _getBodyStructurePartType($aPart)
	{
		$iType = C_MIME_BODYSTRUCTURE_TYPE_NONE;
		if (is_array($aPart) && count($aPart) > 3)
		{
			$sFirstLowerItem = is_string($aPart[0]) ? strtolower($aPart[0]) : '';
			$sSecondLowerItem = is_string($aPart[1]) ? strtolower($aPart[1]) : '';
			if ('"text"' === $sFirstLowerItem)
			{
				$iType = C_MIME_BODYSTRUCTURE_TYPE_TEXT_PLAIN;
				if ('"html"' === $sSecondLowerItem)
				{
					$iType = C_MIME_BODYSTRUCTURE_TYPE_TEXT_HTML;
				}
				
				if (is_array($aPart[2]))
				{
					foreach ($aPart[2] as $mParam)
					{
						$sLoverName = is_string($mParam) ? strtolower($mParam) : '';
						if ('"name"' === $sLoverName ||'"filename"' === $sLoverName)
						{
							$iType = C_MIME_BODYSTRUCTURE_TYPE_ATTACHMENT;
						}
					}
				}
			}
			else if (('"message"' === $sFirstLowerItem && '"rfc822"' === $sSecondLowerItem)
				|| ('"image"' === $sFirstLowerItem && is_string($aPart[3]) && 0 < strlen($aPart[3])))
			{
				$iType = C_MIME_BODYSTRUCTURE_TYPE_ATTACHMENT;
			}
			else
			{
				if (is_array($aPart[2]))
				{
					foreach ($aPart[2] as $mParam)
					{
						$sLoverName = is_string($mParam) ? strtolower($mParam) : '';
						if ('"name"' === $sLoverName || '"filename"' === $sLoverName)
						{
							$iType = C_MIME_BODYSTRUCTURE_TYPE_ATTACHMENT;
						}
					}
				}

				if (isset($aPart[8]) && is_array($aPart[8]))
				{
					foreach ($aPart[8] as $mParam)
					{
						if (is_array($mParam))
						{
							foreach($mParam as $value)
							{
								$sLoverName = is_string($value) ? strtolower($value) : '';
								if ('"name"' === $sLoverName|| '"filename"' === $sLoverName)
								{
									$iType = C_MIME_BODYSTRUCTURE_TYPE_ATTACHMENT;
								}
							}
						}
						else
						{
							$sLoverName = is_string($mParam) ? strtolower($mParam) : '';
							if ('"attachment"' === $sLoverName)
							{
								$iType = C_MIME_BODYSTRUCTURE_TYPE_ATTACHMENT;
							}
						}
					}
				}
			}
		}
		
		return $iType;
	}


	/**
	 * @return array
	 */
	public function GetPlainMimeIndexs()
	{
		return $this->_aPlainIndexs;
	}

	/**
	 * @return array
	 */
	public function GetHtmlMimeIndexs()
	{
		return $this->_aHtmlIndexs;
	}

	/**
	 * @return array
	 */
	public function GetAttachmentMimeIndexs()
	{
		return $this->_aAttachmentsIndexs;
	}
}
